﻿--[[
    @Brief  帐号列表处理
    @Author jameszhai
    @Date   2017-05-24
--]]

EAccCbbRes = 
{
    sDropDnNrl = "ONEIMAGE:client_login\\down_normal.png;Stretch;0";
    sDropDnHov = "ONEIMAGE:client_login\\down_hover.png;Stretch;0";
    sDropDnClk = "ONEIMAGE:client_login\\down_normal.png;Stretch;0";

    sDropUpNrl = "ONEIMAGE:client_login\\up_normal.png;Stretch;0";
    sDropUpHov = "ONEIMAGE:client_login\\up_hover.png;Stretch;0";
    sDropUpClk = "ONEIMAGE:client_login\\up_normal.png;Stretch;0";

    -- 帐号列表的背景
    sAccListBG = "9IMAGES:client_login\\login_input.png?41,427,61,447;"..   -- 第一行
                 "client_login\\login_input.png?65,427,85,447;" ..
                 "client_login\\login_input.png?89,427,109,447;" ..
                 "client_login\\login_input.png?41,451,61,471;"  ..         -- 中间
                 "client_login\\login_input.png?65,451,85,471;" ..
                 "client_login\\login_input.png?89,451,109,471;" ..
                 "client_login\\login_input.png?41,475,61,495;"  ..         -- 最后一行
                 "client_login\\login_input.png?65,475,85,495;" ..
                 "client_login\\login_input.png?89,475,109,495;" .. 
                 "Stretch;0;";
    sAccEditBG = "ONEIMAGE:client_login\\login_input.png?40,331,221,362;Stretch;0;";

    -- ITEM的背景
    sTopItemBG = "ONEIMAGE:client_login\\login_input.png?239,425,420,456;Stretch;0;";
    sMidItemBG = "ONEIMAGE:client_login\\login_input.png?239,467,420,498;Stretch;0;";
    sBtmItemBG = "ONEIMAGE:client_login\\login_input.png?239,506,420,537;Stretch;0;";

};

EAccCbbOverSeaRes = 
{
    -- 帐号列表的背景
    sAccListBG = "ONEIMAGE:client_login\\client_login.png?12,283,22,376;Stretch;0;";
    sAccEditBG = "NONE";

    -- ITEM的背景
    sMidItemBG = "ONEIMAGE:client_login\\client_login.png?27,283,37,314;Stretch;0;";
    sBtmItemBG = "ONEIMAGE:client_login\\client_login.png?27,283,37,314;Stretch;0;";
};

TAccComboBox =
{
    GetListBG = function ()
        local img = EAccCbbRes.sAccListBG;
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            img = EAccCbbOverSeaRes.sAccListBG;
        end
        return img;
    end;

    GetAccEditBG = function ()
        local img = EAccCbbRes.sAccEditBG;
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            img = EAccCbbOverSeaRes.sAccEditBG;
        end
        return img;
    end;

    GetMidItemBG = function ()
        local img = EAccCbbRes.sMidItemBG;
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            img = EAccCbbOverSeaRes.sMidItemBG;
        end
        return img;
    end;

    GetBtmItemBG = function ()
        local img = EAccCbbRes.sBtmItemBG;
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            img = EAccCbbOverSeaRes.sBtmItemBG;
        end
        return img;
    end;

    GetMaxChar = function ()
        local max_char = TAccComboBox.MAXCHAR;
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            max_char = TAccComboBox.MAXCHAROS;
        end
        return max_char;
    end;

    InitView = function(nRootID, nLoginType)
        TAccComboBox.nLoginType = nLoginType;
        TAccComboBox.pAccView = ui_utility.get_ctrl_ptr_by_id(nRootID);

        if nil == nRootID then
            TGPLog.LogE("AccComboBox", "Get account panel fail.");
            return false;
        end;

        TAccComboBox.pAccEdit = ui_utility.get_ctrl_ptr_by_path(nRootID, "pnlUsrAcc");

        if nil == TAccComboBox.pAccEdit then
            TGPLog.LogE("AccComboBox", "Get account edit fail.");
            return false;
        end;

        TAccComboBox.nBtnDrop = ui_utility.get_ctrl_id_by_path(nRootID, "pnlUsrAcc\\btnDrop");

        if nil == TAccComboBox.nBtnDrop or 0 == TAccComboBox.nBtnDrop then
            TGPLog.LogE("AccComboBox", "Drop button is nil.");
            return false;
        end;
        ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "OnClick", "TAccComboBox.OnClkDrop");

        TAccComboBox.pAccList = ui_utility.get_ctrl_ptr_by_path(nRootID, "lsAccList");
        if nil == TAccComboBox.pAccList then
            TGPLog.LogE("AccComboBox", "Get account list fail.");
            return false;
        end;

        local   nTempID = TAccComboBox.pAccList:GetControlID();

        ui_utility.setproperty_by_id(nTempID, "OnItemSelect", "TAccComboBox.OnSelItem");
        ui_utility.setproperty_by_id(nTempID, "OnLoseFocus", "TAccComboBox.OnLostFocus");
        ui_utility.setproperty_by_id(nTempID, "OnItemScroll", "TAccComboBox.OnItemScroll");

        return true;
    end;

    UnInitView = function()
        TAccComboBox.nBtnDrop = 0;
        TAccComboBox.pAccList = nil;
        TAccComboBox.zDelMap  = nil;
    end;

    OnClkDrop = function(pUILib, nCtrlID)
        if nil ~= TAccComboBox.pAccList then
            if TRUE == TAccComboBox.pAccList:IsVisible() then
                TAccComboBox.HideList();
            else
                TAccComboBox.ShowList();
            end;
        else
            TGPLog.LogE("AccComboBox", "Click dropdown, account list is nil.");
        end;
    end;

    OnItemEnter = function(pUILib, dwBtnID)
        if TAccComboBox.nHovItem == dwBtnID then
            return;
        end;

        if nil == pUILib then
            TGPLog.LogE("AccComboBox", "Leave item, uilib is null.");
            return;
        end;

        local   pTemp = pUILib:GetControlPtr(dwBtnID);
        if nil == pTemp then
            TGPLog.LogE("AccComboBox", "Leave item, get button fail.");
            return;
        end;

        local   pItem = pTemp:GetParent();

        if (nil == pItem) then
            TGPLog.LogE("AccComboBox", "Leave item, get parent fail.");
            return;
        end;

        local   nIcon = ui_utility.get_ctrl_id_by_path(pItem:GetControlID(), "pnlMask");

        if nil ~= nIcon and 0 ~= nIcon and TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin == TAccComboBox.nLoginType then
            ui_utility.setproperty_by_id(nIcon, "Background", 
                "ONEIMAGE:client_login\\head_cover_small.png;Stretch;0;");

            TAccComboBox.nHovItem = dwBtnID;
        else
            TGPLog.LogE("AccComboBox", "Select item, get icon fail.");
        end;
    end;

    OnItemLeave = function(pUILib, dwBtnID)
        if nil == pUILib then
            TGPLog.LogE("AccComboBox", "Leave item, uilib is null.");
            return;
        end;

        local   pTemp = pUILib:GetControlPtr(dwBtnID);
        if nil == pTemp then
            TGPLog.LogE("AccComboBox", "Leave item, get button fail.");
            return;
        end;

        local   pItem = pTemp:GetParent();

        if (nil == pItem) then
            TGPLog.LogE("AccComboBox", "Leave item, get parent fail.");
            return;
        end;

        local   nIcon = ui_utility.get_ctrl_id_by_path(pItem:GetControlID(), "pnlMask");

        if nil ~= nIcon and 0 ~= nIcon and TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin == TAccComboBox.nLoginType then
            ui_utility.setproperty_by_id(nIcon, "Background", 
                "ONEIMAGE:client_login\\head_cover_small.png;Stretch;0;");
            TAccComboBox.nHovItem = 0;
        else
            TGPLog.LogE("AccComboBox", "Item lost focus, get icon fail.");
        end;
    end;

    -- 更新列表
    UpdateList = function(zList)
        if nil == TAccComboBox.pAccList then
            TGPLog.LogE("AccComboBox", "Account list control is null.");
            return false;
        end;

        if nil == zList then
            TGPLog.LogE("AccComboBox", "Account list is null.");
            return false;
        end;

        local   pUILib = ui_utility.uilib();

        if nil == pUILib then
            TGPLog.LogE("AccComboBox", "Get UILib fail.");
            return false;
        end;

        TAccComboBox.zDeleteMap = {};

        local   sIcon = U8toCUIStr("pnlIcon");
        local   sNick = U8toCUIStr("btnNick");
        local   sDel  = U8toCUIStr("btnDel");
        local   nCount= zList.count;

        TAccComboBox.nHovItem = 0;
        TAccComboBox.pAccList:SetItemCount(nCount);

        -- 更新帐号列表
        for nIdx = 0, nCount - 1 do
            local   zItem = zList.list[nIdx];

            if nil == zItem then
                TGPLog.LogE("AccComboBox", "Get account item fail:" .. tostring(nIdx));
                break;
            end;

            local   nItem = TAccComboBox.pAccList:GetItemRootIDByIdx(nIdx);
            local   nIcon = pUILib:GetControlIDbyPath(sIcon, nItem);
            local   sHeadIcon = "";
            local   sNickName = "";
            if TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin == TAccComboBox.nLoginType then
                sHeadIcon = "ONEIMAGE:client_login\\nav_tab_icon_email_off.png;Stretch;0;";
                sNickName = zItem.acc_name;
            else
                if (nil ~= zItem.head) and (TAccComboBox.ValidFile(zItem.head)) then
                    sHeadIcon = "ONEIMAGE:" .. zItem.head .. ";Circle;0;";
                else
                    sHeadIcon = "ONEIMAGE:client_login\\qqacc_icon.png;Stretch;0;";
                end;
                sNickName = tostring(zItem.uin);
            end

            if nil ~= nIcon and 0 ~= nIcon then
                ui_utility.setproperty_by_id(nIcon, "Background", "NONE");
                ui_utility.setproperty_by_id(nIcon, "Background", sHeadIcon);
            else
                TGPLog.LogE("AccComboBox", "Get icon panel fail.");
            end;

            local   nNick = pUILib:GetControlIDbyPath(sNick, nItem);
            if nil ~= nNick and 0 ~= nNick then
                local shortNick = sNickName;
                if ui_utility.is_over_char_count(sNickName, TAccComboBox.GetMaxChar()) then
                    shortNick = string.sub(sNickName, 1, TAccComboBox.GetMaxChar()) .. "...";
                    ui_utility.setproperty_by_id(nNick, "TextTip", sNickName);
                end
                TAccComboBox.mapBtnID2Acc[nNick] = sNickName;
                ui_utility.setproperty_by_id(nNick, "Caption", shortNick);
                ui_utility.setproperty_by_id(nNick, "OnClick", "TAccComboBox.OnClkItem");
                ui_utility.setproperty_by_id(nNick, "OnMouseHover", "TAccComboBox.OnItemEnter");
                ui_utility.setproperty_by_id(nNick, "OnMouseMove", "TAccComboBox.OnItemEnter");
                ui_utility.setproperty_by_id(nNick, "OnMouseLeave", "TAccComboBox.OnItemLeave");

                if (2 == nIdx) or (nCount - 1 == nIdx) then
                    ui_utility.setproperty_by_id(nNick, "BackgroundHover",
                                                 TAccComboBox.GetBtmItemBG());
                    ui_utility.setproperty_by_id(nNick, "BackgroundPressed",
                                                 TAccComboBox.GetBtmItemBG());
                else
                    ui_utility.setproperty_by_id(nNick, "BackgroundHover",
                                                 TAccComboBox.GetMidItemBG());
                    ui_utility.setproperty_by_id(nNick, "BackgroundPressed",
                                                 TAccComboBox.GetMidItemBG());
                end;
            else
                TGPLog.LogE("AccComboBox", "Get nick button fail.");
            end;

            local   nDel  = pUILib:GetControlIDbyPath(sDel , nItem);
            if nil ~= nDel and 0 ~= nDel then
                TAccComboBox.zDeleteMap[nDel] = sNickName;
                ui_utility.setproperty_by_id(nDel, "OnClick", "TAccComboBox.OnClkDel");
            else
                TGPLog.LogE("AccComboBox", "Get delete button fail.");
            end;
        end;

        return true;
    end;

    OnClkItem = function(pUILib, nBtnID)
        TAccComboBox.HideList();

        local   zParam = {};

        zParam["uilib"  ] = tonumber(pUILib);
        zParam["ctrl_id"] = tonumber(nBtnID);
        zParam["login_type"] = TAccComboBox.nLoginType;
        zParam["acc"] = TAccComboBox.mapBtnID2Acc[nBtnID];
        comm_center.send_broadcast("Msg_ClkAccItem", zParam);
    end;

    OnClkDel = function(pUILib, nBtnID)
        if nil == TAccComboBox.zDeleteMap then
            TGPLog.LogE("AccLogin", "Delete account, map is null.");
            return;
        end;

        if TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin == TAccComboBox.nLoginType then
            local sAccName = TAccComboBox.zDeleteMap[nBtnID];
            if nil ~= sAccName then
                local   zParam = {};

                zParam["acc_name"] = sAccName;
                comm_center.call_service("Svr_DelMailSaveAcc", zParam);
            else
                TGPLog.LogE("AccLogin", "On delete account, get acc name fail.");
            end
        else
            local nUin = TAccComboBox.zDeleteMap[nBtnID];

            if nil ~= nUin then
                local   zParam = {};

                zParam["uin"] = tonumber(nUin);
                comm_center.call_service("Svr_DelSaveAcc", zParam);
            else
                TGPLog.LogE("AccLogin", "On delete account, get uin fail.");
            end;
        end


        TAccComboBox.ShowList();
    end;

    OnSelItem = function(pUILib, nCtrlID, nItemID)
        local   zParam = {};

        zParam["uilib"  ] = pUILib;
        zParam["ctrl_id"] = nItemID;
        comm_center.send_broadcast("Msg_ClkAccItem", zParam);
    end;

    OnLostFocus = function(pUILib, nCtrlID)
        TAccComboBox.HideList();
    end;

    -- 滚动
    OnItemScroll = function(pUILib, nCtrlID, nFstIdx, nLstIdx)
        if nil == pUILib then
            TGPLog.LogE("AccLogin", "OnItemScroll, uilib is null.");
            return;
        end;

        if nil == TAccComboBox.pAccList then
            TGPLog.LogE("AccLogin", "OnItemScroll, Account list is null.");
            return;
        end;

        if (nFstIdx > nLstIdx) then
            nFstIdx = nLstIdx;
        end;

        local   sNick = U8toCUIStr("btnNick");

        for nIdx = nFstIdx, nLstIdx do
            local   nItem = TAccComboBox.pAccList:GetItemRootIDByIdx(nIdx);

            if nil ~= nItem and 0 ~= nItem then
                local   nNick = pUILib:GetControlIDbyPath(sNick, nItem);

                if nil ~= nNick and 0 ~= nNick then
                    if nIdx == nLstIdx then
                        ui_utility.setproperty_by_id(nNick, "BackgroundHover",
                                                     TAccComboBox.GetBtmItemBG());
                        ui_utility.setproperty_by_id(nNick, "BackgroundPressed",
                                                     TAccComboBox.GetBtmItemBG());
                    else
                        ui_utility.setproperty_by_id(nNick, "BackgroundHover",
                                                     TAccComboBox.GetMidItemBG());
                        ui_utility.setproperty_by_id(nNick, "BackgroundPressed",
                                                     TAccComboBox.GetMidItemBG());
                    end;
                end;
            else
                TGPLog.LogE("AccLogin", "Get item root ID fail.");
            end;
        end;
    end;

    ShowList = function()
        if nil == TAccComboBox.pAccView or nil == TAccComboBox.pAccList then
            TGPLog.LogE("AccComboBox", "Show list, list is nil.");
            return;
        end;

        local list_id = TAccComboBox.pAccView:GetControlID();
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            list_id = TAccComboBox.pAccList:GetControlID();
        end
        ui_utility.setproperty_by_id(list_id, "Background", TAccComboBox.GetListBG());

        if FALSE == TAccComboBox.pAccList:IsVisible() then
            local   zParam = {};
            zParam["show"] = 1;
            comm_center.send_broadcast("Msg_AccListShow", zParam);
        end;

        local   nHeight = 0;

        if TAccComboBox.bAutoSize then
            local   nCount  = TAccComboBox.pAccList:GetItemCount();

            if (nCount <= 0) then
                nHeight = 31;
            elseif nCount < 3 then
                nHeight = nCount * 31;
            else
                nHeight = 93;
            end;
        else
            nHeight = 93;
        end;

        local   rtTemp = TAccComboBox.pAccView:GetRectEx(FALSE,TRUE);

        rtTemp.bottom = rtTemp.top + nHeight + 31;
        TAccComboBox.pAccView:MoveEx(rtTemp,TRUE);

        ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "Background",
                                    EAccCbbRes.sDropUpNrl);
        ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "BackgroundHover",
                                    EAccCbbRes.sDropUpHov);
        ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "BackgroundPressed",
                                    EAccCbbRes.sDropUpClk);

        rtTemp.right  = rtTemp.right  - rtTemp.left;
        rtTemp.left   = 0;
        rtTemp.top    = 31;
        rtTemp.bottom = rtTemp.top + nHeight;
        TAccComboBox.pAccList:MoveEx(rtTemp,TRUE);
        TAccComboBox.pAccList:Show(TRUE);
        TAccComboBox.pAccList:SetFocus();
    end;

    HideList = function()
        if nil ~= TAccComboBox.pAccView then
            local   rtTemp = TAccComboBox.pAccView:GetRectEx(FALSE,TRUE);
            rtTemp.bottom = rtTemp.top + 31;
            TAccComboBox.pAccView:MoveEx(rtTemp,TRUE);

            ui_utility.setproperty_by_id(TAccComboBox.pAccView:GetControlID(),
                                         "Background", TAccComboBox.GetAccEditBG());
        else
            TGPLog.LogE("AccComboBox", "Hide list, account view is null.");
        end;

        if nil ~= TAccComboBox.pAccList then
            ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "Background",
                                        EAccCbbRes.sDropDnNrl);
            ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "BackgroundHover",
                                        EAccCbbRes.sDropDnHov);
            ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "BackgroundPressed",
                                        EAccCbbRes.sDropDnClk);
            TAccComboBox.pAccList:Show(FALSE);
        else
            TGPLog.LogE("AccComboBox", "Hide list, list is nil.");
        end;

        local   zParam = {};
        zParam["show"] = 0;
        comm_center.send_broadcast("Msg_AccListShow", zParam);
    end;

    -- 验证文件是否有效
    ValidFile = function(sFile)
        if nil == sFile then
            return false;
        end;
        local fileSize = ierd_tgp.common.LuaGetFileSize(sFile);
        if fileSize > 0 then
            return true;
        end
        return false;
    end;

    ShowDropBtn = function (bShow)
        if true == bShow then
            ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "Visible", "true");
        else
            ui_utility.setproperty_by_id(TAccComboBox.nBtnDrop, "Visible", "false");
        end
    end;

    bAutoSize= true;        -- 自动改变列表大小
    nBtnDrop = 0;
    nHovItem = 0;
    pAccView = nil;         -- 账号输入框
    pAccList = nil;         -- 账号列表
    pAccEdit = nil;
    zDelMap  = nil;
    nLoginType = 0;
    MAXCHAR  = 13;
    MAXCHAROS = 22;
    mapBtnID2Acc = {};
};